/**
 * 简化验证器
 * 专注于内容分发平台的核心需求
 */

import { IContent } from '../types/adapters';

/**
 * 验证结果接口
 */
export interface IValidationResult {
  valid: boolean;
  errors: string[];
  warnings?: string[];
}

/**
 * 内容验证器
 */
export class ContentValidator {
  /**
   * 验证内容对象
   */
  static validate(content: IContent): IValidationResult {
    const errors: string[] = [];
    const warnings: string[] = [];

    // 验证标题
    if (!content.title || content.title.trim().length === 0) {
      errors.push('标题不能为空');
    } else if (content.title.length > 100) {
      warnings.push('标题过长，建议控制在100字符以内');
    }

    // 验证内容
    if (!content.content || content.content.trim().length === 0) {
      errors.push('内容不能为空');
    } else if (content.content.length > 50000) {
      warnings.push('内容过长，可能影响发布效果');
    }

    // 验证摘要
    if (content.summary && content.summary.length > 500) {
      warnings.push('摘要过长，建议控制在500字符以内');
    }

    // 验证标签
    if (content.tags && content.tags.length > 10) {
      warnings.push('标签数量过多，建议控制在10个以内');
    }

    return {
      valid: errors.length === 0,
      errors,
      warnings
    };
  }

  /**
   * 验证标题
   */
  static validateTitle(title: string): IValidationResult {
    const errors: string[] = [];
    const warnings: string[] = [];

    if (!title || title.trim().length === 0) {
      errors.push('标题不能为空');
    } else {
      if (title.length > 100) {
        warnings.push('标题过长');
      }
      if (title.length < 5) {
        warnings.push('标题过短');
      }
    }

    return {
      valid: errors.length === 0,
      errors,
      warnings
    };
  }
}

/**
 * 文件验证器
 */
export class FileValidator {
  /**
   * 验证文件
   */
  static validate(file: File, options?: {
    maxSize?: number;
    allowedTypes?: string[];
  }): IValidationResult {
    const errors: string[] = [];
    const warnings: string[] = [];
    const maxSize = options?.maxSize || 10 * 1024 * 1024; // 10MB
    const allowedTypes = options?.allowedTypes || [
      'image/jpeg',
      'image/png',
      'image/gif',
      'image/webp'
    ];

    // 验证文件大小
    if (file.size > maxSize) {
      errors.push(`文件大小超过限制 (${this.formatSize(maxSize)})`);
    }

    // 验证文件类型
    if (!allowedTypes.includes(file.type)) {
      errors.push(`不支持的文件类型: ${file.type}`);
    }

    // 验证文件名
    if (!file.name || file.name.trim().length === 0) {
      errors.push('文件名不能为空');
    }

    return {
      valid: errors.length === 0,
      errors,
      warnings
    };
  }

  /**
   * 验证图片文件
   */
  static validateImage(file: File): IValidationResult {
    return this.validate(file, {
      maxSize: 5 * 1024 * 1024, // 5MB
      allowedTypes: ['image/jpeg', 'image/png', 'image/gif', 'image/webp']
    });
  }

  /**
   * 格式化文件大小
   */
  private static formatSize(bytes: number): string {
    if (bytes === 0) return '0 B';
    
    const k = 1024;
    const sizes = ['B', 'KB', 'MB', 'GB'];
    const i = Math.floor(Math.log(bytes) / Math.log(k));
    
    return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
  }
}

/**
 * URL 验证器
 */
export class URLValidator {
  /**
   * 验证 URL 格式
   */
  static validate(url: string): IValidationResult {
    const errors: string[] = [];

    if (!url || url.trim().length === 0) {
      errors.push('URL不能为空');
      return { valid: false, errors };
    }

    try {
      new URL(url);
    } catch {
      errors.push('URL格式不正确');
    }

    return {
      valid: errors.length === 0,
      errors
    };
  }

  /**
   * 检查是否为HTTPS
   */
  static isHttps(url: string): boolean {
    try {
      return new URL(url).protocol === 'https:';
    } catch {
      return false;
    }
  }
}

/**
 * 通用验证器
 */
export class CommonValidator {
  /**
   * 验证邮箱格式
   */
  static validateEmail(email: string): IValidationResult {
    const errors: string[] = [];
    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;

    if (!email || email.trim().length === 0) {
      errors.push('邮箱不能为空');
    } else if (!emailRegex.test(email)) {
      errors.push('邮箱格式不正确');
    }

    return {
      valid: errors.length === 0,
      errors
    };
  }

  /**
   * 验证非空字符串
   */
  static validateRequired(value: string, fieldName: string): IValidationResult {
    const errors: string[] = [];

    if (!value || value.trim().length === 0) {
      errors.push(`${fieldName}不能为空`);
    }

    return {
      valid: errors.length === 0,
      errors
    };
  }

  /**
   * 验证字符串长度
   */
  static validateLength(
    value: string,
    fieldName: string,
    min?: number,
    max?: number
  ): IValidationResult {
    const errors: string[] = [];
    const warnings: string[] = [];

    if (min !== undefined && value.length < min) {
      errors.push(`${fieldName}长度不能少于${min}个字符`);
    }

    if (max !== undefined && value.length > max) {
      errors.push(`${fieldName}长度不能超过${max}个字符`);
    }

    return {
      valid: errors.length === 0,
      errors,
      warnings
    };
  }
}
